package cn.edu.cug.cs.gtl.renders.openinventor.projectors;

import cn.edu.cug.cs.gtl.apis.render.basics.*;
import cn.edu.cug.cs.gtl.apis.render.projectors.Projector;
import cn.edu.cug.cs.gtl.renders.openinventor.basics.MatrixImpl;
import cn.edu.cug.cs.gtl.renders.openinventor.basics.Vec3fImpl;
import com.openinventor.inventor.SbLine;
import com.openinventor.inventor.SbMatrix;
import com.openinventor.inventor.SbVec2f;
import com.openinventor.inventor.SbViewVolume;
import com.openinventor.inventor.projectors.SbProjector;

public class ProjectorImpl implements Projector {
    protected SbProjector nativeObject =null;


    public ProjectorImpl(SbProjector p){
        nativeObject = p;
    }
    public Object getNativeObject() {
        return nativeObject;
    }

    @Override
    public boolean attach(Object nativeObject) {
        if(nativeObject instanceof SbProjector){
            this.nativeObject = (SbProjector) nativeObject;
            return true;
        }
        return false;
    }

    @Override
    public Object detach() {
        Object r = this.nativeObject;
        this.nativeObject=null;
        return r;
    }

    @Override
    public ViewVolume getViewVolume() {
        return null;
    }

    @Override
    public Projector copy() {
        return new ProjectorImpl(this.nativeObject.copy());
    }

    @Override
    public Matrix getWorkingSpace() {
        return new MatrixImpl(this.nativeObject.getWorkingSpace());
    }

    /**
     * Applies the projector using the given line in world coordinates, returning the point in three dimensions that it projects to.
     *
     * @param line
     * @return
     */
    @Override
    public Vec3f intersect(Line line) {
        return new Vec3fImpl(this.nativeObject.intersect((SbLine) line.getNativeObject()));
    }

    /**
     * Apply the projector using the given point, returning the point in three dimensions that it projects to.
     *
     * @param point
     * @return
     */
    @Override
    public Vec3f project(Vec2f point) {
        return new Vec3fImpl(this.nativeObject.project((SbVec2f) point.getNativeObject()));
    }

    @Override
    public void setViewVolume(ViewVolume vol) {
        this.nativeObject.setViewVolume((SbViewVolume) vol.getNativeObject());
    }

    @Override
    public void setWorkingSpace(Matrix space) {
        this.nativeObject.setWorkingSpace((SbMatrix) space.getNativeObject());
    }
}
